package org.jeecg.modules.weixin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.reserve.entity.SdxcReserve;
import org.jeecg.modules.reserve.entity.SdxcReservePlace;
import org.jeecg.modules.reserve.entity.SdxcReservePlaceRecord;
import org.jeecg.modules.reserve.entity.SdxcReserveUser;
import org.jeecg.modules.reserve.entity.vo.SdxcReservePlaceVo;
import org.jeecg.modules.reserve.service.ISdxcReservePlaceRecordService;
import org.jeecg.modules.reserve.service.ISdxcReservePlaceService;
import org.jeecg.modules.reserve.service.ISdxcReserveService;
import org.jeecg.modules.reserve.service.ISdxcReserveUserService;
import org.jeecg.modules.reserve.utils.QiniuUpload;
import org.jeecg.modules.weixin.constant.WxConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@Slf4j
    @RestController
    @Api (tags = "预约管理微信端")
    @RequestMapping ("/weixin")
    public class WxController {

        @Autowired
        RestTemplate restTemplate;

        @Autowired
        private ISdxcReserveUserService sdxcReserveUserService;

        @Autowired
        private ISdxcReservePlaceService placeService;

        @Autowired
        private ISdxcReservePlaceRecordService placeRecordService;

        @Autowired
        private ISdxcReserveService reserveService;
        /**
         * 微信小程序用户登录获取用户openid
         *
         * @param
         * @return
         */
        @GetMapping ("/login")
        @ApiOperation (value = "用户授权登录", notes = "用户授权登录")
        public Result<?> login(@ApiParam (name = "code", value = "授权code", required = true) @RequestParam ("code") String code) {
            String url = WxConstant.LOGIN_URL.replace("APPID", WxConstant.APP_ID).replace("SECRET", WxConstant.APP_SECRET).replace("JSCODE",code);

            log.info("授权登录请求url====={}" , url);
            String resStr = restTemplate.getForObject(url, String.class);
            JSONObject resJson = JSON.parseObject(resStr);
            log.info("授权登录响应========={}", resJson);

            if(null == resJson || resJson.containsKey("errcode")) {
                return Result.error("登录失败");
            }
            //  获取用户openid
            String openid = resJson.getString("openid");
            if(StringUtils.isBlank(openid)) {
                return Result.error("openid获取失败，请重试");
            }
            // 保存用户信息

            String userId = sdxcReserveUserService.saveByOpenId(openid);
            return Result.ok(userId);

        }

        /**
         * 判断用户信息是否已完善
         *
         * @param
         * @return
         */
        @GetMapping("/queryUserStatus")
        public Result<?> queryUserStatus(@RequestParam("userId") String userId) {
            SdxcReserveUser sdxcReserveUser = sdxcReserveUserService.getById(userId);
            if (sdxcReserveUser != null && SdxcReserveUser.USER_INFO_FINISH_YES.equals(sdxcReserveUser.getIsFinish())) {
                // 用户信息已完善
                return Result.ok(SdxcReserveUser.USER_INFO_FINISH_YES);
            } else {
                // 用户不存在或信息未完善
                return Result.ok(SdxcReserveUser.USER_INFO_FINISH_NO);
            }
        }
        /**
         * 保存用户信息
         *
         * @param sdxcReserveUser
         * @return
         */
        @PutMapping("/saveUserInfo")
        public Result<?> saveUserInfo(@RequestBody SdxcReserveUser sdxcReserveUser) {
            if(StringUtils.isBlank(sdxcReserveUser.getId())) {
                return Result.error("用户ID为空");
            }
            SdxcReserveUser save = sdxcReserveUserService.getById(sdxcReserveUser.getId());
            if(null == save) {
                return Result.error("用户信息不存在");
            }
            // 状态为信息已完善
            sdxcReserveUser.setIsFinish(SdxcReserveUser.USER_INFO_FINISH_YES);
            sdxcReserveUserService.updateById(sdxcReserveUser);
            return Result.ok("保存成功!");
        }
        /**
         * 获取场地使用明细
         *
         * @return
         */
        @GetMapping("/getPlaceDetail")
        public Result<?> getPlaceDetail() {
            // 查询所有可用场地
            QueryWrapper<SdxcReservePlace> queryWrapper = new QueryWrapper();
            queryWrapper.eq("status", "1");
            List<SdxcReservePlace> palceList = placeService.selectList(queryWrapper);
            // 返回结果
            List<SdxcReservePlaceVo> placeVoList = new ArrayList<>();
            if(null != palceList && palceList.size() > 0) {
                for(SdxcReservePlace place : palceList) {
                    String dateBeg = DateUtils.formatDate(new Date(),"yyyyMMdd");
                    String dateEnd = DateUtils.formatDate(DateUtils.addDate(new Date(),1),"yyyyMMdd");
                    // 查询当天预约记录
                    List<SdxcReservePlaceRecord> recordListBeg = placeRecordService.queryRecordDetail(place.getId(), dateBeg);
                    placeVoList.add(setReserveVo(recordListBeg,place, new Date()));

                    // 查询第二天预约记录
                    List<SdxcReservePlaceRecord> recordListEnd = placeRecordService.queryRecordDetail(place.getId(), dateEnd);
                    placeVoList.add(setReserveVo(recordListEnd,place, DateUtils.addDate(new Date(),1)));
                }
            }
            return  Result.ok(placeVoList);
        }

        /**
         * 组装前端场地明细数据
         *
         * @param recordList
         * @param place
         * @param date
         * @return
         */
        private SdxcReservePlaceVo setReserveVo(List<SdxcReservePlaceRecord> recordList, SdxcReservePlace place, Date date) {
            SdxcReservePlaceVo placeVo = new SdxcReservePlaceVo();
            placeVo.setPlaceId(place.getId());
            placeVo.setPlaceName(place.getName());
            placeVo.setTotal(Integer.toString(place.getPlaceLimit()));
            if(null != recordList && recordList.size() > 0) {
                placeVo.setIsUse(Integer.toString(recordList.get(0).getIsReserve()));
                placeVo.setRemain(Integer.toString(place.getPlaceLimit() - recordList.get(0).getUsed()));
            } else { // 当天无记录
                placeVo.setIsUse("1");
                placeVo.setRemain(Integer.toString(place.getPlaceLimit()));
            }
            String reserveWeek = DateUtils.dateToWeek(date);
            String reserveDate = DateUtils.formatDate(date,"MM") + "月" + DateUtils.formatDate(date,"dd") + "日";
            placeVo.setReserveWeek(reserveWeek);
            placeVo.setReserveDate(reserveDate+"("+ reserveWeek +")");

            placeVo.setDate(DateUtils.formatDate(date,"yyyy-MM-dd"));
            return placeVo;
        }
    /**
     * 查询用户当日预约记录，判断当日是否可预约，
     * 如果当日该场地已有审核中或审核通过的数据，提示用户不可预约
     *
     * @param userId 用户id
     * @param placeId 场地id
     * @param date 入场时间
     * @return
     */
    @GetMapping("/queryReserve")
    public Result<?> queryReserve(@RequestParam("userId") String userId,
                                  @RequestParam("placeId") String placeId,
                                  @RequestParam("date") String date) {
        QueryWrapper<SdxcReserve> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("place_id", placeId);
        Date now = new Date();
        try {
            now = DateUtils.parseDate(date,"yyyy-MM-dd");
        } catch (ParseException e) {
        }
        queryWrapper.eq("entry_time", now);
        queryWrapper.in("status", 1, 2);
        SdxcReserve sdxcReserve = reserveService.getOne(queryWrapper);
        if (null != sdxcReserve) {
            return Result.error("当日该场地已有预约记录，请勿重复预约");
        }
        return Result.ok();
    }
    /**
     * 上传健康码 到七牛云
     *
     * @param request
     * @param response
     * @return
     */
    @PostMapping(value = "/upload")
    public Result<?> upload(HttpServletRequest request, HttpServletResponse response) {
        try {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            // 获取上传文件对象
            MultipartFile mfile = multipartRequest.getFile("file");
            // 获取图片格式
            String suffix = mfile.getOriginalFilename().substring(mfile.getOriginalFilename().lastIndexOf('.'));
            // 文件名 uuid+图片格式
            String fileName = UUID.randomUUID().toString().replace("-", "") + suffix;
            // 上传图片到七牛云 返回访问全路径
            String dbpath = QiniuUpload.updateFile(mfile, fileName);
            return Result.ok(dbpath);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error(e.getMessage());
        }
    }
    /**
     * 保存预约记录
     *
     * @param sdxcReserve
     * @return
     */
    @PutMapping("/saveReserve")
    public Result<?> saveReserve(@RequestBody SdxcReserve sdxcReserve) {
        String entryDate = sdxcReserve.getEntryDate();
        try {
            sdxcReserve.setEntryTime(DateUtils.parseDate(entryDate,"yyyy-MM-dd"));
        } catch (ParseException e) {
            return Result.error("预约日期有误");
        }
        sdxcReserve.setId(UUID.randomUUID().toString().replace("-",""));
        // 审核中
        sdxcReserve.setStatus(2);
        reserveService.save(sdxcReserve);
        return Result.ok("保存成功!");
    }
    /**
     * 获取七牛云私有空间下载路径
     *
     * @param baseUrl
     * @return
     */
    @GetMapping("/getDownloadUrl")
    public Result<?> getDownloadUrl(@RequestParam(name = "baseUrl") String baseUrl) {
        String downloadUrl = QiniuUpload.getDownloadUrl(baseUrl);
        if(!StringUtils.isBlank(downloadUrl)) {
            return Result.ok(downloadUrl);
        }
        return Result.error("下载地址获取失败");
    }
    }

